package com.hengtiansoft.lfy.adapter;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hengtiansoft.lfy.R;
import com.hengtiansoft.lfy.utils.CommonUtils;
import com.hengtiansoft.lfy.utils.Kdxf_TTSPlayer;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @ClassName: CollectAdapter
 * @Description: 口译ListView
 * @author caijiajun
 * @date 2015年10月14日 下午5:23:26
 * 
 */
public class CollectAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private ArrayList<HashMap<String, Object>> mList;
    CollectAdapterListener mListener;
    private int mLastPosition;
    ViewHolder holder = null;

    public void setAdapterListener(CollectAdapterListener listener) {
        this.mListener = listener;
    }

    public CollectAdapter(Context context,
                          ArrayList<HashMap<String, Object>> list) {
        mContext = context;
        this.mList = list;
        mInflater = LayoutInflater.from(mContext);
        mLastPosition = -1;
    }

    public final class ViewHolder {
        TextView mTvText;
        TextView mTvTranslation;
        LinearLayout linear_player_translation;
        LinearLayout linear_player_text;
        ImageButton mBtnPlayerText;
        ImageButton mBtnPlayerTranslation;
        ImageButton mIbtnCopy;
     //   ImageButton mIbtnTranspond;
        ImageButton mIbtnDelete;
        ImageButton mIbtnCollect;
        ImageButton mIbtnOther;
        RelativeLayout mRl;
        ImageButton mIbtnFullScreen;
        ImageView iv_click;

    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public HashMap<String, Object> getItem(int position) {
        return mList.get(position);
    }
    AnimationDrawable anim;
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.fragment_conversation_interpret_listview_item,
                    null);
            holder.mTvText = (TextView) convertView.findViewById(R.id.tv_text);
            holder.mTvTranslation = (TextView) convertView
                    .findViewById(R.id.tv_translation);
            holder.linear_player_text= (LinearLayout) convertView.findViewById(R.id.linear_player_text);
            holder.linear_player_translation= (LinearLayout) convertView.findViewById(R.id.linear_player_translation);
            holder.mBtnPlayerText = (ImageButton) convertView
                    .findViewById(R.id.ibtn_player_text);
            holder.mBtnPlayerTranslation = (ImageButton) convertView
                    .findViewById(R.id.ibtn_player_translation);
            holder.mIbtnCopy = (ImageButton) convertView
                    .findViewById(R.id.ibtn_copy);
//            holder.mIbtnTranspond = (ImageButton) convertView
//                    .findViewById(R.id.ibtn_transpond);
            holder.iv_click= (ImageView) convertView.findViewById(R.id.iv_click);
            holder.mIbtnCollect = (ImageButton) convertView
                    .findViewById(R.id.ibtn_collect);
            holder.mIbtnDelete = (ImageButton) convertView
                    .findViewById(R.id.ibtn_delete);
            holder.mIbtnOther = (ImageButton) convertView
                    .findViewById(R.id.ibtn_other);
            holder.mRl = (RelativeLayout) convertView
                    .findViewById(R.id.rl_other);
            holder.mIbtnFullScreen = (ImageButton) convertView
                    .findViewById(R.id.ibtn_is_full_screen);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.mIbtnCollect.setVisibility(View.GONE);
        if (mLastPosition == position) {
            if (holder.mRl.getVisibility() == View.GONE) {
                holder.mRl.setVisibility(View.VISIBLE);
            } else {
                holder.mRl.setVisibility(View.GONE);
                mLastPosition = -1;
            }
            holder.iv_click.setImageResource(R.drawable.i_down);
        } else {
            holder.mRl.setVisibility(View.GONE);
            holder.iv_click.setImageResource(R.drawable.i_right);
        }
        final HashMap<String, Object> item = getItem(position);
        holder.mTvText.setText(item.get("text").toString());
        holder.mTvTranslation.setText(item.get("translation").toString());
        holder.mIbtnCollect.setVisibility(View.GONE);

        holder.linear_player_translation.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (CommonUtils.isNetWorkConnected(mContext)) {
                    String synText = item.get("translation").toString();
                    if (synText.length() <= 0)
                        return;
                    if (anim != null) {
                        anim.stop();
                        anim.selectDrawable(0);
                    }
                    ImageView ibtn_player_translation = null;
                    if (v.findViewById(R.id.ibtn_player_translation) != null) {
                        ibtn_player_translation = (ImageView) v.findViewById(R.id.ibtn_player_translation);
                    }
                    if (ibtn_player_translation == null) {
                        return;
                    }
                    anim = (AnimationDrawable) ibtn_player_translation.getBackground();
                    Kdxf_TTSPlayer.getInstense().setStopListener(new Kdxf_TTSPlayer.StopListener() {
                        @Override
                        public void stop() {
                            anim.stop();
                            anim.selectDrawable(0);
                        }
                    });
                    anim.start();
                    Kdxf_TTSPlayer.getInstense().TTSPlay(synText, "", "");

                } else {
                    Toast.makeText(mContext, mContext.getString(R.string.network_error), Toast.LENGTH_LONG)
                            .show();
                }
            }
        });
        holder.linear_player_text.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (CommonUtils.isNetWorkConnected(mContext)) {
                    String synText = item.get("text").toString();
                    if (synText.length() <= 0)
                        return;
                    if (anim != null) {
                        anim.stop();
                        anim.selectDrawable(0);
                    }
                    ImageView ibtn_player_text = null;
                    if (v.findViewById(R.id.ibtn_player_text) != null) {
                        ibtn_player_text = (ImageView) v.findViewById(R.id.ibtn_player_text);
                    }
                    if (ibtn_player_text == null) {
                        return;
                    }
                    anim = (AnimationDrawable) ibtn_player_text.getBackground();
                    Kdxf_TTSPlayer.getInstense().setStopListener(new Kdxf_TTSPlayer.StopListener() {
                        @Override
                        public void stop() {
                            anim.stop();
                            anim.selectDrawable(0);
                        }
                    });
                    anim.start();
                    Kdxf_TTSPlayer.getInstense().TTSPlay(synText, "", "");

                } else {
                    Toast.makeText(mContext,  mContext.getString(R.string.network_error), Toast.LENGTH_LONG)
                            .show();
                }
            }
        });

       /* holder.mTvTranslation.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (CommonUtils.isNetWorkConnected(mContext)) {
                    String synText = item.get("translation").toString();
                    if (synText.length() <= 0)
                        return;
                    // TTSPlayer.getInstense().TTSPlay(synText);
                    Kdxf_TTSPlayer.getInstense().TTSPlay(synText, "", "");
                } else {
                    Toast.makeText(mContext, "请检查网络！", Toast.LENGTH_LONG)
                            .show();
                }
            }
        });
        holder.mBtnPlayerTranslation.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (CommonUtils.isNetWorkConnected(mContext)) {
                    String synText = item.get("translation").toString();
                    if (synText.length() <= 0)
                        return;
                    // TTSPlayer.getInstense().TTSPlay(synText);
                    Kdxf_TTSPlayer.getInstense().TTSPlay(synText,"","");
                } else {
                    Toast.makeText(mContext, "请检查网络！", Toast.LENGTH_LONG)
                            .show();
                }
            }
        });*/
       /* holder.mTvText.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (CommonUtils.isNetWorkConnected(mContext)) {
                    String synText = item.get("text").toString();
                    if (synText.length() <= 0)
                        return;

                    // TTSPlayer.getInstense().TTSPlay(synText);
                    Kdxf_TTSPlayer.getInstense().TTSPlay(synText,"","");
                } else {
                    Toast.makeText(mContext, "请检查网络！", Toast.LENGTH_LONG)
                            .show();
                }
            }
        });
        holder.mBtnPlayerText.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (CommonUtils.isNetWorkConnected(mContext)) {
                    String synText = item.get("text").toString();
                    if (synText.length() <= 0)
                        return;

                    //TTSPlayer.getInstense().TTSPlay(synText);
                    Kdxf_TTSPlayer.getInstense().TTSPlay(synText,"","");
                } else {
                    Toast.makeText(mContext, "请检查网络！", Toast.LENGTH_LONG)
                            .show();
                }
            }
        });*/

        holder.mIbtnCopy.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.onCopyClick(position);
            }
        });
//        // 转发
//        holder.mIbtnTranspond.setOnClickListener(new OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                if (mListener != null)
//                    mListener.onTranspondClick(position);
//            }
//        });
        holder.iv_click.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View view) {
                if (mListener != null)
                    mListener.onItemClick(position,view);
            }
        });
        holder.mIbtnDelete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.onDeleteClick(position);
            }
        });
        holder.mIbtnOther.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.onOtherClick(position);
            }
        });
        holder.mIbtnFullScreen.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.onFullScreenClick(position);
            }
        });
     /*   holder.linear_player_translation.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                // TODO Auto-generated method stub
                if (mListener != null)
                    mListener.onItemLongClick(position);
                return false;
            }
        });
        holder.linear_player_text.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                // TODO Auto-generated method stub
                if (mListener != null)
                    mListener.onItemLongClick(position);
                return false;
            }
        });
        convertView.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                // TODO Auto-generated method stub
                if (mListener != null)
                    mListener.onItemLongClick(position);
                return false;
            }
        });*/
//        convertView.setOnClickListener(new OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                // TODO Auto-generated method stub
//                if (mListener != null)
//                    mListener.onItemClick(position);
//            }
//        });
        return convertView;
    }

    // 显示和隐藏item下面的按钮
    public void changeImageVisable(int position) {
        mLastPosition = position;
    }

    public interface CollectAdapterListener {

        public void onCopyClick(int position);

        public void onTranspondClick(int position);

        public void onCollectClick(int position);

        public void onNotCollectClick(int position);

        public void onDeleteClick(int position);

        public void onOtherClick(int position);

        public void onItemClick(int position,View view);

        public void onFullScreenClick(int position);

        //public void onItemLongClick(int position);
    }
}
